package cn.edu.hzvtc.service.impl;

import cn.edu.hzvtc.dao.AreaMapper;
import cn.edu.hzvtc.dao.UserMapper;
import cn.edu.hzvtc.pojo.Area;
import cn.edu.hzvtc.pojo.User;
import cn.edu.hzvtc.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class userServiceImpl implements userService {
    @Autowired
    private UserMapper userMapper;
    private AreaMapper areaMapper;

    public userServiceImpl(AreaMapper areaMapper) {
        this.areaMapper = areaMapper;
    }

    @Override
    public int add(User user) {
        return userMapper.insert(user);
    }

    //登录验证
    public User login(String userName, String userPassword) {
        User result = null;
        List<User> users = userMapper.selectByNameAndPassword(userName,
                DigestUtils.md5DigestAsHex(userPassword.getBytes()));
        if (users.size() > 0) result = users.get(0);
        return result;
    }


    //获取登录用户地址信息
    @Override
    public User loginUser(Integer id) {
//        Area area=areaMapper.selectByPrimaryKey(user.getUserAreaId());
//        Area tArea=area;
//        Integer areaParentId=area.getAreaParentId();
//        while(areaParentId!=null){
//            Area pArea=areaMapper.selectByPrimaryKey(areaParentId);
//            tArea.setParentArea(pArea);
//            areaParentId=pArea.getAreaParentId();
//            tArea=pArea;
//        }
//        user.setArea(area);
//        return user;
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> getAreaAdmins(Integer provinceId, Integer cityId, Integer schoolId, String userName) {
        List<User> result = null;
        result = userMapper.selectAreaAdmins(provinceId, cityId, schoolId, userName);
        return result;
    }

    @Override
    public Boolean deleteAreaAdmin(String ids) {
        Boolean result = false;

        if (ids.contains("-")) {
            //批量删除
            String[] str_ids = ids.split("-");
            List<Integer> list_ids = new ArrayList<Integer>();
            for (String str_id : str_ids) {
                list_ids.add(Integer.parseInt(str_id));
            }
            userMapper.delStatesByList(list_ids);
            result = true;
        } else {
            //普通删除
            Integer id = Integer.parseInt(ids);
            userMapper.delStateByPrimaryKey(id);
            result = true;
        }
        return result;
    }


    //添加新用户
    public Integer addAreaAdmin(User user) {
        //对用户密码进行加密
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        return userMapper.insert(user);
    }

    //通过传入id值获取用户area信息
    public User getAreaAdmin(Integer id) {
        return userMapper.selectAreaAdminById(id);
    }


    //修改用户信息
    public Integer updateAreaAdmin(User user) {
        User areaAdminUpdate = userMapper.selectByPrimaryKey(user.getId());
        areaAdminUpdate.setUserName(user.getUserName());
        if (user.getUserAreaId() != null)
            areaAdminUpdate.setUserAreaId(user.getUserAreaId());
        return userMapper.updateByPrimaryKey(areaAdminUpdate);
    }

    public Integer resetPassword(Integer id) {
        return userMapper.resetPasswordByPrimaryKey(id);
    }

    //计数
    public Long getCount(Integer userAreaId) {
        return userMapper.getCount(userAreaId);
    }


    //根据院校、学院和用户名查询
    public List<User> getUsers(Integer unitTypeId, Integer unitId, String userName,Integer userAreaId) {
        List<User> result = null;
        result = userMapper.selectUsers(unitTypeId, unitId, userName,userAreaId);
        return result;
    }

    //根据id，删除用户
    @Override
    public Boolean  deleteUser(String ids) {
        Boolean result = false;

        if (ids.contains("-")) {
            //批量删除
            String[] str_ids = ids.split("-");
            List<Integer> list_ids = new ArrayList<Integer>();
            for (String str_id : str_ids) {
                list_ids.add(Integer.parseInt(str_id));
            }
            userMapper.delStatesByList(list_ids);
            result = true;
        } else {
            //普通删除
            Integer id = Integer.parseInt(ids);
            userMapper.delStateByPrimaryKey(id);
            result = true;
        }
        return result;
    }

    //添加新用户
    @Override
    public Integer addUser(User user) {
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        return userMapper.insert(user);
    }

    //根据id，查询用户
    @Override
    public User getUser(Integer id) {
        return userMapper.selectUserById(id);
    }

    //根据id，修改用户
    public Integer updateUser(User user) {
        User userUpdate = userMapper.selectByPrimaryKey(user.getId());
        userUpdate.setUserName(user.getUserName());
        if (user.getUserAreaId() != null)
            userUpdate.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes()));
        return userMapper.updateByPrimaryKey(userUpdate);
    }
    //修改用户部门分配
    @Override
    public Integer updateUserUnit(Integer userId, List<Integer> unitIds) {
        userMapper.deleteUnitsByUserId(userId);
        return userMapper.insertUnitsByUserId(userId,unitIds);
    }

}
